program q123;
var
    n, s, t, ans, i, c: longint;
begin
    assign(input, '');
    assign(output, '');
    reset(input);
    rewrite(output);

    readln(c);
    for i:=1 to c do
    begin
        readln(s, n);

        ans := s; t := 0;
        while (t < n) do
        begin
            if ((ans mod 4 = 0) and (ans mod 100 <> 0)) or (ans mod 400 = 0) then
                inc(t);
            inc(ans);
        end;

        writeln(ans-1);
    end;

    close(input);
    close(output);
end.
